
import { formatDate, urlToFileWithFormData } from "@/units/index.js";

export function decryptArticle() {
    const titleInfo = document.querySelectorAll('header > div  > h1')[0].innerHTML
    console.log(titleInfo, 'titleInfo');
    // urlToBase64('https://img.decrypt.co/insecure/rs:fit:3840:0:0:0/plain/https://cdn.decrypt.co/wp-content/uploads/2025/05/Bitcoin-decrypt-style-03-gID_7.png@webp').then(res => {
    //     console.log(res, 'ssssssssssssssseeeeeeeeeeeeee');

    // })
    // const authorHtml = document.querySelector('.header > div  > div')
    // console.log(authorHtml, 'htmmmmmmmmllllllll');
    const authorInfo = document.querySelector('.underline > a').innerHTML
    console.log(authorInfo, 'autjorrrrrrrr');
    const timeInfo = formatDate(document.querySelectorAll('header > div  > div > div > div > span > time')[0].innerHTML.split('<')[0])
    console.log(timeInfo, 'timeInfo');
    const maincontent = document.querySelector('.post-content')
    console.log(maincontent, 'ooooooooooooo');
    // let maincontent = ''
    // mainContentItem.forEach(item => {
    //     maincontent += item.outerHTML
    // })
    console.log(document.querySelector('figure > div > img').src, 'wwwwwwwwwwwwwwwwwwww');

    urlToFileWithFormData(document.querySelector('figure > div > img').src, 'asset.png').then(formData => {
        const file = formData.get('file');
        file.arrayBuffer().then(buffer => {
            chrome.runtime.sendMessage({
                type: "proxy",
                method: "POST",
                url: "/dex/upload",
                fileName: file.name,
                fileType: file.type,
                fileBuffer: Array.from(new Uint8Array(buffer))
            }, (response) => {
                const imgElement = `<img fetchpriority="high" src="${response.data}" width="696" height="522" sizes="(max-width: 696px) 100vw, 696px" alt="${titleInfo}" decoding="async" nitro-lazy-empty="" id="NzQyOjM3-1">`
                // 将请求图片上传的接口返回的值进行赋值
                // imgElement.src = ''
                const content = imgElement + maincontent
                const params = {
                    author: authorInfo,
                    category: '1',
                    content: content,
                    cover: '',
                    publishDate: timeInfo,
                    source: 'https://decrypt.co',
                    status: 0,
                    title: titleInfo,
                    type: '1'
                }
                console.log(params, '上传参数');
                // 获取token并请求保存接口
                chrome.storage.local.get('token', (data) => {
                    const token = data.token;
                    chrome.runtime.sendMessage({
                        type: "proxy",
                        method: "POST",
                        url: "/dex/dex/news/save",
                        body: params,
                        token: token
                    }, (saveResponse) => {
                        console.log(saveResponse, '新闻保存结果');
                    });
                });
            });
        });
    }).catch(error => {
        console.error('上传图片失败:', error);
    });
}